package com.pi.tcp.util;

import com.pi.base.enumerate.charset.CommCharset;
import com.pi.base.util.lang.ByteUtil;

public class PiBedInsEnum {
  private static final byte INS_HEADER = 0x76;

  public static byte[] generatePasswordIns(
      String deiceId, String wifiSSid, String password) throws Exception{
    byte[] deviceIdB = ByteUtil.hexToByte(deiceId);
    byte[] wifiSSidB = wifiSSid.getBytes(CommCharset.UTF_8.getValue());
    byte[] passwordB = password.getBytes(CommCharset.UTF_8.getValue());
    int dataLength = wifiSSidB.length + passwordB.length + 4;
    int totalLength = dataLength + deviceIdB.length + 3;
    byte[] ins = new byte[totalLength];
    ins[0] = INS_HEADER;
    System.arraycopy(deviceIdB, 0, ins, 1, deviceIdB.length);
    ins[deviceIdB.length + 1] = (byte)(dataLength & 0xFF);
    int wifiStartIndex =  deviceIdB.length + 2;
    ins[wifiStartIndex] = 0x01;
    ins[wifiStartIndex + 1] = (byte)(wifiSSidB.length & 0xFF);
    System.arraycopy(wifiSSidB, 0, ins, wifiStartIndex + 2, wifiSSidB.length);
    int passwordStartIndex =  wifiStartIndex + 2 + wifiSSidB.length;
    ins[passwordStartIndex] = 0x02;
    ins[passwordStartIndex + 1] = (byte)(passwordB.length & 0xFF);
    System.arraycopy(passwordB, 0, ins, passwordStartIndex + 2, passwordB.length);
    ins[totalLength -1] = checkSum(ins);
    return ins;
  }
  
  /**
   * @description 计算校验和
   * @param b  默认数组的最后一位是放校验和的，所以计算时要-1
   * @return
   */
  private static byte checkSum(byte[] b){
    int sum = 0;
    for(int i=0; i< b.length-1; i++){
      sum += b[i];
    }
    sum = sum % 256;
    return (byte)(sum & 0xFF);
  }
  
  /**
   * @description 计算校验和
   * @param b  默认数组的最后一位是放校验和的，所以计算时要-1
   * @return
   */
  private static byte[] checkSum(byte[] b, int start, int length){
    int sum = 0;
    for(int i= start; i< start + length; i++){
      sum += b[i];
    }
    byte[] crc = new byte[2];
    crc[0] = (byte)(sum >> 8);
    crc[1] = (byte)sum;
    return crc;
  }
  
  
  public static byte[] generateTimeIns(){
    byte[] timeSync = new byte[12];
    timeSync[0] = (byte)0xFE;
    timeSync[1] = (byte)0x09;
    timeSync[2] = (byte)0x01;
    timeSync[3] = (byte)0x00;
    timeSync[4] = (byte)0x05;
    timeSync[5] = (byte)0x00;
    long time = System.currentTimeMillis() / 1000;
    timeSync[6] = (byte)(time >> 24);
    timeSync[7] = (byte)(time >> 16);
    timeSync[8] = (byte)(time >> 8);
    timeSync[9] = (byte)time;
    byte[] crc = checkSum(timeSync, 5, 0x05);
    timeSync[10] = crc[0];
    timeSync[11] = crc[1];
    return timeSync;
  }
  
  public static byte[] generateWifiIns(String ssid, String password, String domain){
    byte[] timeSync = new byte[10 + ssid.length() + password.length() + domain.length()];
    timeSync[0] = (byte)0xFE;
    timeSync[1] = (byte)0x01;
    timeSync[2] = (byte)0xF6;
    timeSync[3] = (byte)0x00;
    timeSync[4] = (byte)(ssid.length() + password.length() + domain.length() + 3);
    timeSync[5] = (byte)ssid.length();
    System.arraycopy(ssid.getBytes(), 0, timeSync, 6, ssid.length());
    timeSync[ssid.length() + 6] = (byte) password.length();
    System.arraycopy(password.getBytes(), 0, timeSync, ssid.length() + 7, password.length());
    timeSync[ssid.length() + password.length() + 8] = (byte) domain.length();
    System.arraycopy(domain.getBytes(), 0, timeSync, ssid.length() + password.length() + 9, domain.length());
    byte[] crc = checkSum(timeSync, 5, ssid.length() + domain.length() + 3);
    timeSync[timeSync.length-2] = crc[0];
    timeSync[timeSync.length-1] = crc[1];
    System.out.println("wifi指令 " + new String(timeSync));
    return timeSync;
  }
  
  public static byte[] generateUploadSuccessIns(){
    byte[] timeSync = new byte[12];
    timeSync[0] = (byte)0xFE;
    timeSync[1] = (byte)0x09;
    timeSync[2] = (byte)0x02;
    timeSync[3] = (byte)0x00;
    timeSync[4] = (byte)0x05;
    timeSync[5] = (byte)0x00;
    long time = System.currentTimeMillis() / 1000;
    timeSync[6] = (byte)(time >> 24);
    timeSync[7] = (byte)(time >> 16);
    timeSync[8] = (byte)(time >> 8);
    timeSync[9] = (byte)time;
    byte[] crc = checkSum(timeSync, 5, 0x05);
    timeSync[10] = crc[0];
    timeSync[11] = crc[1];
    return timeSync;
  }
}
